package com.publicize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.publicize.commom.servicebase.exception.MyException;
import com.publicize.entity.Module;
import com.publicize.entity.ModulePicture;
import com.publicize.entity.User;
import com.publicize.mapper.ModuleMapper;
import com.publicize.service.ModulePictureService;
import com.publicize.service.ModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.publicize.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 投稿信息表 服务实现类
 * </p>
 *
 * @author bs
 * @since 2021-03-04
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements ModuleService {
    @Resource
    private UserService userService;
    @Resource
    private ModuleService moduleService;
    @Autowired
    private ModulePictureService modulePictureService;
    //根据模块id查看对应稿子
    @Override
    public List<Module> selectModuleById(String moduleId, Integer page, Integer limit) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("relationship_id",moduleId);
        //查询已通过稿子
        queryWrapper.eq("status",1);
        //分页
        Page<Module> pages=new Page<>(page,limit);
        IPage<Module> moduleIPage = baseMapper.selectPage(pages, queryWrapper);
        //展示分页数据
        List<Module> records = moduleIPage.getRecords();
        return records;
    }
    //根据用户id查看对应稿子
    @Override
    public List<Module> selectModuleByUserIdAndRelationId(String userId,String relationId) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        //查询所有对应稿子
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("relationship_id",relationId);
        List<Module> modules = baseMapper.selectList(queryWrapper);
        return modules;
    }
//用户投稿
    @Override
    public boolean saveModule(Module module) {
        //插入稿子
        int insert = baseMapper.insert(module);
        if(insert!=1) throw new MyException("插入失败",201);
        User user=new User();
        user.setId(module.getUserId());
        user=userService.getById(user);
        //对用户表中的投稿数量加一
        user.setUserEssayNumber(user.getUserEssayNumber()+1);
        boolean update = userService.updateById(user);
        if(!update) throw new MyException("投稿失败",201);
        return true;
    }
//用户删除稿子
    @Override
    public boolean deleteModule(String moduleId) {
        boolean module = moduleService.removeById(moduleId);
        if(!module) throw new MyException("删除稿子失败",201);
        QueryWrapper<ModulePicture> modulePictureQueryWrapper=new QueryWrapper<>();
        modulePictureQueryWrapper.eq("module_id",moduleId);
        boolean remove=modulePictureService.remove(modulePictureQueryWrapper);
        return remove;
    }

    @Override
    public Integer countModuleByRelationId(String relation) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("relationship_id",relation);
        queryWrapper.eq("status",1);
        return baseMapper.selectCount(queryWrapper);
    }
}
